/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(['N/search', 'N/record'], function (search, record) {
        function getInputData() {
            var mySearch = initSearch();
            var alls = [];
            var results = mySearch.run().getRange(0, 1000);
            var j = 1;
            while (results.length > 0 && j < 2) {
                for (var i = 0; i < results.length; i++) {
                    var result = results[i];
                    var tmp = {};
                    tmp.id = result.id;
                    alls[alls.length] = tmp;
                }
                results = mySearch.run().getRange(j * 1000, 1000 + j * 1000);
                j++;
            }
            log.debug('alls', JSON.stringify(alls));
            return alls;
        }

        function map(context) {
            var manufacturingcosttemplateId = JSON.parse(context.value).id;
            var rec = record.load({type: 'manufacturingcosttemplate', id: manufacturingcosttemplateId, isDynamic: true});
            var modulus = Number(rec.getValue('custrecord_modulus'));


            var mySearch = search.create({
                type: 'customrecord_monthmanufactureexpense',
                columns: ['custrecord_estimatedtotalmanuexpense', 'custrecord_estimatedtotalqty_curtains', 'custrecord_enable_update'],
                filters: [
                    {
                        "name": "custrecord_enable_update",
                        "operator": "is",
                        "values": [
                            "T"
                        ],
                        "isor": false,
                        "isnot": false,
                        "leftparens": 0,
                        "rightparens": 0
                    }
                ],
            });
            var result = mySearch.run().getRange(0, 1);
            log.debug('result', result);
            if (result.length > 0) {
                var estimatedtotalmanuexpense = Number(result[0].getValue('custrecord_estimatedtotalmanuexpense'));
                var estimatedtotalqtycurtains = Number(result[0].getValue('custrecord_estimatedtotalqty_curtains'));
                var newManufacturefee = (estimatedtotalmanuexpense / estimatedtotalqtycurtains * modulus).toFixed(2);
                rec.setValue('custrecord_manufacturefee', newManufacturefee);
                var recId = rec.save();
                log.audit('更新成功', '成本模板:' + recId + ',新的制造费用为:' + newManufacturefee);

            }
        }


        function reduce(context) {
        }

        function summarize(summary) {
        }

        function initSearch() {
            var mySearch = search.create({
                type: 'manufacturingcosttemplate',
                filters: [['custrecord_processtype', 'is', 4]
                    , 'and', ['internalid', 'is', 123]
                ]
            });
            return mySearch;
        }


        return {
            getInputData: getInputData,
            map: map,
            reduce: reduce,
            summarize: summarize
        };
    }
);